package com.snail.controller;

import com.snail.model.pojo.mall.BizProduct;

import com.snail.model.pojo.mall.StockDecreaseRequest;
import com.snail.result.Result;
import com.snail.service.IBizProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class BizProductController {

    @Autowired
    private IBizProductService BizProductServiceImpl;

    /**
     * 查询所有未删除商品（deleted=0）
     * 接口地址：GET /product/undelete
     */
    @GetMapping("/undelete")
    public Result<List<BizProduct>> getUndeleteProducts() {
        List<BizProduct> products = BizProductServiceImpl.listUndeleteProducts();
        return Result.success(products); // 用统一响应类封装结果
    }
    @PostMapping("/decreaseStock")
    public Result<?> decreaseStock(@RequestBody StockDecreaseRequest request) {
        return BizProductServiceImpl.decreaseStock(request.getProductId(), request.getQuantity());
    }
    @PostMapping("/decreaseStocks")
    public Result<?> decreaseStocks(@RequestBody List<StockDecreaseRequest> requests) {
        for (StockDecreaseRequest request : requests) {
            BizProductServiceImpl.decreaseStock(request.getProductId(), request.getQuantity());
        }
        return Result.success();
    }

    /**
     * 新增商品
     * 请求方式：POST /api/product/add
     * 请求体：{"name": "xxx", "price": 100, ...}
     */
    @PostMapping("/add")
    public Result<?> addProduct(@RequestBody BizProduct product) {
        return BizProductServiceImpl.addProduct(product);
    }

    /**
     * 修改商品
     * 请求方式：PUT /api/product/update
     * 请求体：{"id": 1, "name": "新名称", ...}
     */
    @PutMapping("/update")
    public Result<?> updateProduct(@RequestBody BizProduct product) {
        return BizProductServiceImpl.updateProduct(product);
    }

    /**
     * 逻辑删除商品
     * 请求方式：DELETE /api/product/1（1为商品ID）
     */
    @DeleteMapping("/deleteProduct/{id}")
    public Result<?> deleteProduct(@PathVariable("id") Integer productId) {
        return BizProductServiceImpl.deleteProduct(productId);
    }


    /**
     * 根据productId查询商品
     * @param productId 商品ID
     * @return 商品信息（null表示未找到）
     */
    @GetMapping("/{productId}")
    public Result<List<BizProduct>> getProductById(@PathVariable("productId") Integer productId) {
        // 直接调用MyBatis-Plus提供的getById方法
        System.out.println("productId:" );
        System.out.println(productId);
        List<BizProduct> products = BizProductServiceImpl.getProductWithSpecs(productId);
        System.out.println(productId);
        return Result.success(products); // 假设 Result 是统一响应类
    }

    /**
     * 根据sellerUserId查询商家所有商品
     * @param sellerUserId 商家ID
     * @return 所有商品信息（null表示未找到）
     */
    @GetMapping("/seller/{sellerUserId}")
    public Result<List<BizProduct>> getProductsBySellerId(@PathVariable("sellerUserId") Integer sellerUserId) {
        return Result.success(BizProductServiceImpl.listBySellerUserId(sellerUserId));
    }

    //根据商品id查询商品
    @GetMapping("/selectById")
    Result<BizProduct> selectById(@RequestParam Integer productId){
        BizProduct product = BizProductServiceImpl.selectById(productId);
        return Result.success(product);
    }
}